﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.Generic;

namespace WenslyWuLib.Common
{
    /// <summary>
    /// Page 基类
    /// </summary>
    public abstract class PageBase : PhoneApplicationPage
    {
        #region Fields

        private bool _contentLoaded;

        /// <summary>
        /// PageView mapping
        /// </summary>
        private static IDictionary<string, string> _mapping;

        ///// <summary>
        ///// Xml path base string
        ///// </summary>
        //private string _xmlPathBase = "/MTodo;component/";

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        public PageBase()
        {
            InitPageView();
            InitControls();
            AttatchEvent();
            InitCommand();
        }

        /// <summary>
        /// Init page view
        /// </summary>
        public virtual void InitPageView()
        {
            if (_mapping == null)
            {
                _mapping = new Dictionary<string, string>();
                _mapping.Add("Pages.MainPage", "MainPage.xaml");
            }

            if (_contentLoaded)
            {
                return;
            }
            _contentLoaded = true;
            if (!_mapping.ContainsKey(GetXAMLViewName()))
            {
                throw new Exception("View xaml file undefined!");
            }
            else
                System.Windows.Application.LoadComponent(this, new System.Uri("/MTodo;component/Xaml/" + _mapping[GetXAMLViewName()],
                    System.UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// Init Controls
        /// </summary>
        protected virtual void InitControls()
        {

        }

        /// <summary>
        /// Attatch Event
        /// </summary>
        protected virtual void AttatchEvent()
        {

        }
        /// <summary>
        /// Init Command
        /// </summary>
        protected virtual void InitCommand()
        {

        }

        /// <summary>
        /// Page Type
        /// </summary>
        /// <returns></returns>
        protected abstract string GetXAMLViewName();
        
    }
}
